package com.tgy.programmer_code_interview_guide._01_stack_queue;

import java.util.Stack;

/**
 * @Author: tgy
 * @Date: 1/14/21 2:01 PM
 *
 * leetcode url: https://leetcode-cn.com/problems/min-stack/
 *
 *
 *      6    6
 *      4    4
 *      2    2
 *      5    2
 *      3    2
 *      1    1
 *   上面算法使用空间换时间
 */
public class _1_设计一个有getMin功能的栈 {

    private Stack<Integer> stack = new Stack<>();
    private Stack<Integer> minStack = new Stack<>();

    public void push(int x) {

        if (stack.isEmpty()) {

            stack.push(x);
            minStack.push(x);
            return;
        }

        int min = minStack.peek();
        if (x < min) {
            min = x;
        }

        stack.push(x);
        minStack.push(min);
    }

    public void pop() {

        minStack.pop();
        stack.pop();
    }

    public int top() {

        return stack.peek();
    }

    public int getMin() {

        return minStack.peek();
    }

  public static void main(String[] args) {

      _1_设计一个有getMin功能的栈 minStack = new _1_设计一个有getMin功能的栈();


      minStack.push(6);
      minStack.push(4);
      minStack.push(2);
      minStack.push(5);
      minStack.push(3);
      minStack.push(1);

      minStack.pop();
      System.out.println(minStack.getMin());
      minStack.pop();
      System.out.println(minStack.getMin());
  }
}
